package com.project.rfidpage.controller;


import com.github.pagehelper.PageInfo;
import com.project.rfidpage.aop.TagLog;
import com.project.rfidpage.dto.FileParam;
import com.project.rfidpage.dto.LicenseAddParam;
import com.project.rfidpage.dto.LicenseParam;
import com.project.rfidpage.entity.License;
import com.project.rfidpage.service.LicenseService;
import com.project.rfidpage.util.ExcelUtil;
import com.project.rfidpage.util.RestResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/rfidpage/license/")
public class LicenseController extends BaseController {

    @Resource
    LicenseService licenseService;

    /**
     * 列表查询
     */
    @PostMapping("page")
    @TagLog(operation = 0, content = "查询制卡列表")
    public RestResult page(@RequestBody LicenseParam param) {
        this.getPage(true, param);
        return RestResult.ok(new PageInfo<>(licenseService.getListForPage(param)));
    }

    /**
     * 导出列表
     */
    @PostMapping("output")
    @TagLog(operation = 5, content = "导出制卡列表")
    public RestResult output(@RequestBody LicenseParam param) {
        this.getPage(false, param);
        return RestResult.ok(licenseService.getListForPage(param));
    }

    /**
     * 新增导入
     */
    @PutMapping("input/add")
    @TagLog(operation = 2, content = "新增制卡导入")
    public RestResult inputAdd(@RequestHeader(value = "user") String user, @RequestBody LicenseAddParam param) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowStr = sdf.format(new Date());
            List<License> list = new ArrayList<>();
            for (int i = 0; i < param.getSize(); i++) {
                License license = new License();
                BeanUtils.copyProperties(param, license);
                license.setMakedate(nowStr);
                license.setUpdatetime(nowStr);
                license.setUpdateuser(user);
                license.setMakestatus(0);
                license.setCaruser("");
                license.setLicensenumber("");
                list.add(license);
            }
            licenseService.insert(list);
            List<License> emptyList = licenseService.getEmptyList(0);
            for (License license : emptyList) {
                if (license.getApplytype() < 10000000) {
                    license.setCardid(String.format("%08d", license.getApplytype()));
                } else {
                    license.setCardid(String.valueOf(license.getApplytype()));
                }
            }
            licenseService.update(emptyList);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }

    /**
     * 生成制卡导入
     */
    @PutMapping("input/update")
    @TagLog(operation = 3, content = "生成制卡导入")
    public RestResult inputUpdate(@RequestHeader(value = "user") String user, FileParam param) {
        try {
            List<License> list = new ArrayList<>();
            List<List<String>> excelData = ExcelUtil.getExcelData(param.getFile(), false);
            if (excelData.size() == 0) {
                return RestResult.error("导入的文件数据至少有一行");
            }
            for (int i = 0; i < excelData.size(); i++) {
                License entity = new License();
                if (!StringUtils.isEmpty(excelData.get(i).get(0))) {
                    entity.setCaruser(excelData.get(i).get(0));
                }
                if (!StringUtils.isEmpty(excelData.get(i).get(1))) {
                    entity.setLicensenumber(excelData.get(i).get(1));
                }
                if (!StringUtils.isEmpty(excelData.get(i).get(2))) {
                    entity.setFzdate(excelData.get(i).get(2));
                }
                list.add(entity);
            }
            List<License> emptyList = licenseService.getEmptyList(1);
            if (emptyList.size() == 0) {
                return RestResult.error("没有未分配的卡号");
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowStr = sdf.format(new Date());
            for (License entity : list) {
                for (License empty : emptyList) {
                    if (empty.getMakestatus() == 0) {
                        entity.setApplytype(empty.getApplytype());
                        entity.setCardid(empty.getCardid());
                        entity.setIssuedby(empty.getIssuedby());
                        entity.setLicenssubject(empty.getLicenssubject());
                        entity.setMakedate(empty.getMakedate());
                        entity.setUpdatetime(nowStr);
                        entity.setUpdateuser(user);
                        entity.setMakestatus(1);
                        empty.setMakestatus(1);
                        break;
                    }
                }
            }
            licenseService.update(list);
            return RestResult.ok();
        } catch (Exception e) {
            return RestResult.error(e.getMessage());
        }
    }
}
